package by.gravity.billprinter;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.EditText;
import by.gravity.billprinter.data.SettingsManager;
import by.gravity.billprinter.http.HttpClient;

import com.actionbarsherlock.app.SherlockPreferenceActivity;
import com.ipaulpro.afilechooser.FileChooseActivity;

public class SettingActivity extends SherlockPreferenceActivity {

	private static final int SELECT_CERTIFICATE_PATH = 10;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);

		Preference login = findPreference(getString(R.string.login));
		login.setOnPreferenceClickListener(new OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {

				showCustomDialog(R.string.login);
				return false;
			}
		});

		Preference password = findPreference(getString(R.string.password));
		password.setOnPreferenceClickListener(new OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {

				showCustomDialog(R.string.password);
				return false;
			}
		});

		Preference text1Default = findPreference(getString(R.string.text1Default));
		text1Default.setOnPreferenceClickListener(new OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {

				showCustomDialog(R.string.text1Default);
				return false;
			}
		});

		Preference text2Default = findPreference(getString(R.string.text2Default));
		text2Default.setOnPreferenceClickListener(new OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {

				showCustomDialog(R.string.text2Default);
				return false;
			}
		});

		Preference nameServer = findPreference(getString(R.string.nameServer));
		nameServer.setOnPreferenceClickListener(new OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {

				showCustomDialog(R.string.nameServer, SettingsManager.getServerName());
				return false;
			}
		});

		Preference port = findPreference(getString(R.string.port));
		port.setOnPreferenceClickListener(new OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {

				showCustomDialog(R.string.port, SettingsManager.getPort());
				return false;
			}
		});

		Preference pathToCertificate = findPreference(getString(R.string.pathToCertificate));
		pathToCertificate.setOnPreferenceClickListener(new OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {

				Intent intent = new Intent(SettingActivity.this, FileChooseActivity.class);
				startActivityForResult(intent, SELECT_CERTIFICATE_PATH);
				return false;
			}
		});

		Preference certificatePasswort = findPreference(getString(R.string.certificatePassword));
		certificatePasswort.setOnPreferenceClickListener(new OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {

				showCustomDialog(R.string.certificatePassword);
				return false;
			}
		});

		initSummary();
	}

	@SuppressWarnings("deprecation")
	private void initSummary() {

		Preference login = findPreference(getString(R.string.login));
		login.setSummary(SettingsManager.getLogin());

		Preference password = findPreference(getString(R.string.password));
		password.setSummary(!SettingsManager.getPassword().equals(getString(R.string.empty)) ? convertPassword(SettingsManager.getPassword())
				: getString(R.string.empty));

		Preference text1Default = findPreference(getString(R.string.text1Default));
		text1Default.setSummary(SettingsManager.getText1Default());

		Preference text2Default = findPreference(getString(R.string.text2Default));
		text2Default.setSummary(SettingsManager.getText2Default());

		Preference serverName = findPreference(getString(R.string.nameServer));
		serverName.setSummary(SettingsManager.getServerName());

		Preference port = findPreference(getString(R.string.port));
		port.setSummary(SettingsManager.getPort());

		Preference pathToCertificate = findPreference(getString(R.string.pathToCertificate));
		pathToCertificate.setSummary(SettingsManager.getPathToCertificate());

		Preference certificatePasswort = findPreference(getString(R.string.certificatePassword));
		certificatePasswort.setSummary(!SettingsManager.getCertificatePassword().equals(getString(R.string.empty)) ? convertPassword(SettingsManager
				.getCertificatePassword()) : getString(R.string.empty));
	}

	private String convertPassword(String password) {

		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < password.length(); i++) {
			builder.append("*");
		}

		return builder.toString();
	}

	private void setValue(int key, String value) {

		if (key == R.string.login) {
			SettingsManager.setLogin(value);
		} else if (key == R.string.password) {
			SettingsManager.setPassword(value);
		} else if (key == R.string.text1Default) {
			SettingsManager.setText1Default(value);
		} else if (key == R.string.text2Default) {
			SettingsManager.setText2Default(value);
		} else if (key == R.string.nameServer) {
			SettingsManager.setServerName(value);
		} else if (key == R.string.port) {
			SettingsManager.setPort(value);
		} else if (key == R.string.certificatePassword) {
			SettingsManager.setCertificatePassword(value);
			HttpClient.newInstance(getApplicationContext());
		}

		initSummary();

	}

	private void showCustomDialog(final int title) {

		showCustomDialog(title, null);
	}

	private void showCustomDialog(final int title, String text) {

		AlertDialog.Builder builderInner = new AlertDialog.Builder(this);

		final EditText editText = new EditText(this);
		if (text != null) {
			editText.setText(text);
		}
		builderInner.setView(editText);

		builderInner.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int whichButton) {

				String value = editText.getText().toString();

				setValue(title, value);
			}
		});

		builderInner.setTitle(title);

		builderInner.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int whichButton) {

				dialog.dismiss();
			}
		});

		builderInner.show();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != RESULT_OK) {
			return;
		}

		if (requestCode == SELECT_CERTIFICATE_PATH) {
			SettingsManager.setPathToCertificate(data.getData().toString().replace("file://", ""));
			HttpClient.newInstance(getApplicationContext());
			initSummary();
		}
	}

}
